package cn.edu.zucc.usedcarsales.control;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.zucc.usedcarsales.itf.ISeriesInformationManager;
import cn.edu.zucc.usedcarsales.model.SeriesInformation;
import cn.edu.zucc.usedcarsales.model.VehicleBrand;
import cn.edu.zucc.usedcarsales.util.DBUtil;

public class SeriesInformationManager implements ISeriesInformationManager {

	@Override
	public List<SeriesInformation> getAll() {
		// TODO Auto-generated method stub
		List<SeriesInformation> list = new ArrayList<SeriesInformation>();
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from SeriesInformation");
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				SeriesInformation vt = new SeriesInformation(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5));
				list.add(vt);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return list;
		}
	}

	@Override
	public void add(SeriesInformation info) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("insert into SeriesInformation (BrandID,SeriesName,BelongBrand,SeriesIntroduction) values(?,?,?,?)");
			pst.setInt(1, info.getBrandID());
			pst.setString(2, info.getSeriesName());
			pst.setString(3, info.getBelongBrand());
			pst.setString(4, info.getSeriesIntroduction());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("delete from SeriesInformation where SeriesID = ?");
			pst.setInt(1, id);
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public SeriesInformation get(int id) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from  SeriesInformation where SeriesID=?");
			pst.setInt(1, id);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				SeriesInformation vt = new SeriesInformation(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5));
				return vt;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void update(SeriesInformation info) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("update SeriesInformation set BrandID=?,SeriesName=?,BelongBrand=?,SeriesIntroduction=? where SeriesID=?");
			pst.setInt(1, info.getBrandID());
			pst.setString(2,info.getSeriesName());
			pst.setString(3, info.getBelongBrand());
			pst.setString(4, info.getSeriesIntroduction());
			pst.setInt(5, info.getSeriesID());
			pst.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int getIdByName(String name) {
		// TODO Auto-generated method stub
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from  SeriesInformation where SeriesName=?");
			pst.setString(1, name);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				return rs.getInt("SeriesID");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
		return 0;
	}

	@Override
	public List<SeriesInformation> getAllByBrandId(int brandid) {
		// TODO Auto-generated method stub
		List<SeriesInformation> list = new ArrayList<SeriesInformation>();
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement pst = con.prepareStatement("select * from SeriesInformation where BrandID=?");
			pst.setInt(1, brandid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				SeriesInformation vt = new SeriesInformation(rs.getInt(1),rs.getInt(2),rs.getString(3),rs.getString(4),rs.getString(5));
				list.add(vt);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return list;
		}
	}

}
